package com.ruoyi.web.controller.report;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ReportConfig;
import com.ruoyi.system.domain.ReportFieldConfig;
import com.ruoyi.system.service.IReportConfigService;
import com.ruoyi.system.service.IReportFieldConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报表配置Controller
 * 
 * @author ruoyi
 * @date 2025-10-23
 */
@RestController
@RequestMapping("/report/config")
public class ReportConfigController extends BaseController
{
    @Autowired
    private IReportConfigService reportConfigService;

    @Autowired
    private IReportFieldConfigService reportFieldConfigService;

    /**
     * 查询报表配置列表
     */
    @PreAuthorize("@ss.hasPermi('report:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(ReportConfig reportConfig)
    {
        startPage();
        List<ReportConfig> list = reportConfigService.selectReportConfigList(reportConfig);
        return getDataTable(list);
    }

    /**
     * 导出报表配置列表
     */
    @PreAuthorize("@ss.hasPermi('report:config:export')")
    @Log(title = "报表配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReportConfig reportConfig)
    {
        List<ReportConfig> list = reportConfigService.selectReportConfigList(reportConfig);
        ExcelUtil<ReportConfig> util = new ExcelUtil<ReportConfig>(ReportConfig.class);
        util.exportExcel(response, list, "报表配置数据");
    }

    /**
     * 获取报表配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('report:config:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(reportConfigService.selectReportConfigById(id));
    }

    /**
     * 根据报表编码获取报表配置
     */
    @GetMapping(value = "/code/{reportCode}")
    public AjaxResult getInfoByCode(@PathVariable("reportCode") String reportCode)
    {
        return success(reportConfigService.selectReportConfigByCode(reportCode));
    }

    /**
     * 新增报表配置
     */
    @PreAuthorize("@ss.hasPermi('report:config:add')")
    @Log(title = "报表配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportConfig reportConfig)
    {
        return toAjax(reportConfigService.insertReportConfig(reportConfig));
    }

    /**
     * 修改报表配置
     */
    @PreAuthorize("@ss.hasPermi('report:config:edit')")
    @Log(title = "报表配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportConfig reportConfig)
    {
        return toAjax(reportConfigService.updateReportConfig(reportConfig));
    }

    /**
     * 删除报表配置
     */
    @PreAuthorize("@ss.hasPermi('report:config:remove')")
    @Log(title = "报表配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reportConfigService.deleteReportConfigByIds(ids));
    }

    /**
     * 查询报表字段配置列表
     */
    @PreAuthorize("@ss.hasPermi('report:config:query')")
    @GetMapping("/field/list")
    public TableDataInfo fieldList(ReportFieldConfig reportFieldConfig)
    {
        startPage();
        List<ReportFieldConfig> list = reportFieldConfigService.selectReportFieldConfigList(reportFieldConfig);
        return getDataTable(list);
    }

    /**
     * 根据报表编码获取字段配置
     */
    @GetMapping(value = "/field/code/{reportCode}")
    public AjaxResult getFieldsByCode(@PathVariable("reportCode") String reportCode)
    {
        List<ReportFieldConfig> list = reportFieldConfigService.selectFieldConfigByReportCode(reportCode);
        return success(list);
    }

    /**
     * 根据报表编码获取列表字段配置
     */
    @GetMapping(value = "/field/list/{reportCode}")
    public AjaxResult getListFieldsByCode(@PathVariable("reportCode") String reportCode)
    {
        List<ReportFieldConfig> list = reportFieldConfigService.selectListFieldsByReportCode(reportCode);
        return success(list);
    }

    /**
     * 根据报表编码获取查询字段配置
     */
    @GetMapping(value = "/field/query/{reportCode}")
    public AjaxResult getQueryFieldsByCode(@PathVariable("reportCode") String reportCode)
    {
        List<ReportFieldConfig> list = reportFieldConfigService.selectQueryFieldsByReportCode(reportCode);
        return success(list);
    }

    /**
     * 新增报表字段配置
     */
    @PreAuthorize("@ss.hasPermi('report:config:add')")
    @Log(title = "报表字段配置", businessType = BusinessType.INSERT)
    @PostMapping("/field")
    public AjaxResult addField(@RequestBody ReportFieldConfig reportFieldConfig)
    {
        return toAjax(reportFieldConfigService.insertReportFieldConfig(reportFieldConfig));
    }

    /**
     * 修改报表字段配置
     */
    @PreAuthorize("@ss.hasPermi('report:config:edit')")
    @Log(title = "报表字段配置", businessType = BusinessType.UPDATE)
    @PutMapping("/field")
    public AjaxResult editField(@RequestBody ReportFieldConfig reportFieldConfig)
    {
        return toAjax(reportFieldConfigService.updateReportFieldConfig(reportFieldConfig));
    }

    /**
     * 删除报表字段配置
     */
    @PreAuthorize("@ss.hasPermi('report:config:remove')")
    @Log(title = "报表字段配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/field/{ids}")
    public AjaxResult removeField(@PathVariable Long[] ids)
    {
        return toAjax(reportFieldConfigService.deleteReportFieldConfigByIds(ids));
    }

    /**
     * 批量保存字段配置
     */
    @PreAuthorize("@ss.hasPermi('report:config:edit')")
    @Log(title = "批量保存字段配置", businessType = BusinessType.UPDATE)
    @PostMapping("/field/batch/{reportCode}")
    public AjaxResult batchSaveFields(@PathVariable("reportCode") String reportCode, @RequestBody List<ReportFieldConfig> fieldConfigs)
    {
        return toAjax(reportFieldConfigService.batchSaveFieldConfig(reportCode, fieldConfigs));
    }
}
